#!/usr/bin/env python
#-*-coding:utf-8 -*-

import CGIHTTPServer
import BaseHTTPServer
import counter
from get import getter
import urlparse
import re
class ParserHandler(CGIHTTPServer.CGIHTTPRequestHandler):
    def getUnicode(self, str):
        try:
            return str.decode('utf-8')
        except:
            return str.decode('gbk')
    def clean(self, str):
        rc = re.compile(r'\<script(.+?)\</script\>', re.S)
        rcs = re.compile(r'\<style(.+?)\</style\>', re.S)
        s = rc.sub('<script></script>', str)
        return rcs.sub('<style></style>', s)

    def do_GET(self):
        self.send_response(200, 'OK')
        self.send_header('Content-Type', 'text/html;charset=utf-8')
        self.end_headers()

        u = urlparse.urlparse(self.path)
        querys = u.query.split('&')
        req = dict()
        for item in querys:
            val = item.split('=')
            if len(val) == 2:
                req.setdefault(val[0], '')
                req[val[0]] = val[1]

        if 'url' in req:
            html,header = getter().get(req['url'])
            print header
            p = counter.CountParser()
            #try:
            uhtml = self.getUnicode(html)
            fd = open('data.old', 'w')
            fd.write(uhtml.encode('utf-8'))
            fd.close()
            fd = open('data.res','w')
            fd.write(self.clean(uhtml).encode('utf-8'))
            fd.close()
            p.feed(self.clean(uhtml))
            self.wfile.write(p.getMainContent().encode('utf-8'))
            #except:
            #word = u"这个页面的编码太奇怪了！"
            #self.wfile.write(word.encode('utf-8'))
        print self.path
if __name__ == "__main__":
    svr = BaseHTTPServer.HTTPServer(('0.0.0.0', 8888), ParserHandler)
    svr.serve_forever()
